package com.nulldev.util.JVM.reflections.cache.tests;

import java.util.HashSet;
import java.util.Set;

import com.nulldev.util.JVM.JVM;
import com.nulldev.util.JVM.reflections.Reflections;
import com.nulldev.util.JVM.reflections.cache.CachedReflection;
import com.nulldev.util.io.FastIO.FastIO;

public class GetMethodsTest {

	public static void main(String[] args) throws Throwable {
		JVM.println("Using: DIRECT_CACHED_REFLECTIONS");
		try {
			final Class<?> system = CachedReflection.getClass("java.lang.System");
			final Set<String> out = new HashSet<String>(32);
			CachedReflection.methodNames(out, true, system);
			JVM.println("Methods in System:");
			JVM.println(out);
			JVM.println("Invoke println(\"Hello world!\")");
			final long s = System.currentTimeMillis();
			CachedReflection.invoke(System.out.getClass(), System.out, "println", "Hello world!");
			final long e = System.currentTimeMillis();
			JVM.println("Execution time: " + (e - s) + " ms");
		} catch (Exception x) {
			JVM.errorln(x);
		}
		JVM.println("Using: REFLECTIONS_API (CACHED_REFLECTIONS)");
		try {
			final long s = System.currentTimeMillis();
			final Reflections ref = Reflections.use("java.lang.System");
			ref.EXPERIMENT_USE_CACHED_REFLECTIONS = true;
			JVM.println("Invoke println(\"Hello world!\")");
			final Reflections out = Reflections.encapsulate(ref.declVar("out"));
			out.EXPERIMENT_USE_CACHED_REFLECTIONS = true;
			out.invokeDecl("println", "Hello world!");
			final long e = System.currentTimeMillis();
			JVM.println("Execution time: " + (e - s) + " ms");
		} catch (Exception x) {
			JVM.errorln(x);
		}
		JVM.println("Using: REFLECTIONS_API (LEGACY)");
		try {
			final long s = System.currentTimeMillis();
			final Reflections ref = Reflections.use("java.lang.System");
			JVM.println("Invoke println(\"Hello world!\")");
			final Reflections out = Reflections.encapsulate(ref.declVar("out"));
			out.invokeDecl("println", new Class<?>[]
				{ String.class }, "Hello world!");
			final long e = System.currentTimeMillis();
			JVM.println("Execution time: " + (e - s) + " ms");
		} catch (Exception x) {
			JVM.errorln(x);
		}
		FastIO.get().flush();
		JVM.exit();
	}

}
